package com.mxpio.erp.plan.mrp.policy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mxpio.erp.common.workshop.entity.ManufactureOrder;
import com.mxpio.erp.plan.mrp.Context;
import com.mxpio.erp.plan.mrp.CountModelMap;
import com.mxpio.erp.plan.mrp.bo.CountModel;
import com.mxpioframework.common.util.BeanReflectionUtils;

public abstract class LrpOrderPolicy implements LrpPolicy {
	@Override
	public void run(Context context) {
		before(context);
		apply(context);
		after(context);
	}
	
	@Override
	public void before(Context context) {
		/*
		 * Map<Integer,String> fields = new HashMap<Integer,String>(); List<String>
		 * fieldlist = context.getItemIndexFields(); for(int i =
		 * 0;i<fieldlist.size();i++) { fields.put(i, fieldlist.get(i)); }
		 * 
		 * List<String> result = new ArrayList<String>(); List<List<String>> dic =
		 * getDic(); calculateCombination(dic, 0, new String[dic.size()], result);
		 */
	}
	
	public void indexProcessAdd(Context context,Object entity, CountModelMap<List<CountModel>> result, CountModel countModel) throws Exception {
		String itemCode = "";
		if(entity instanceof ManufactureOrder){
			itemCode = BeanReflectionUtils.getPropertyValue(entity, "productItemCode");
		}else{
			itemCode = BeanReflectionUtils.getPropertyValue(entity, "itemCode");
		}
		
		StringBuffer sb = new StringBuffer();
		//遍历索引
		for(String itemIndexField : context.getItemIndexFields()) {
			sb = sb.append(BeanReflectionUtils.getPropertyValue(entity, itemIndexField)+",");
		}
		if(sb.length() != 0) {
			sb.setLength(sb.length() - 1);
		}
			
		countModel.setIndexes(sb.toString());
		Map<String, List<CountModel>> resultMap = result.get(itemCode);
		if(resultMap == null) {
			resultMap = new HashMap<String, List<CountModel>>();
		}
		
		List<CountModel> countModels = resultMap.get(sb.toString());
		if(countModels == null){
			countModels = new ArrayList<CountModel>();
		}
		
		/*boolean repeat = false;
		CountModel temp = null;
		for(CountModel model : countModels){
			if(model.compareTo(countModel) == 0){
				temp = model;
				repeat = true;
				break;
			}
		}
		if(!repeat){
			countModels.add(countModel);
		}else{
			temp.setCount(temp.getCount().add(countModel.getCount()));
			temp.setRemark(temp.getRemark() + "," + countModel.getRemark());
			temp.setBoCode(temp.getBoCode() + "," + countModel.getBoCode());
		}*/
		
		countModels.add(countModel);
		resultMap.put(sb.toString(), countModels);
		result.put(itemCode, resultMap);
		
	}
	
	
	@Override
	public void after(Context context) {
		
	}

	/**
     * 写法二，递归计算所有组合
     * @param inputList 所有数组的列表，数组用List<String>存储
     * @param beginIndex 代表每一个数组的在inputList中的索引
     * @param fields 数组别名
     * @param arr 用于保存每一次递归生成的组合
     * @param result 结果
     * */
    public void calculateCombination(List<List<String>> inputList, int beginIndex, String[] arr, List<String> result) {
    	if(beginIndex == inputList.size()){
            //在这里进行你自己的处理，比如打印组合的结果
    		StringBuffer sb = new StringBuffer();
            for (String i : arr) {
                sb = sb.append(i+", ");
            }
            sb.setLength(sb.length() - 1);
            result.add(sb.toString());
            return;
        }
        for(String c: inputList.get(beginIndex)){
            arr[beginIndex] = c;
            calculateCombination(inputList, beginIndex + 1, arr, result);
        }
    }
	
	
	/**
     * 非递归计算所有组合
     * @param inputList 所有数组的列表
     * */
	/*
	 * public void calculateCombination(List<List<String>> inputList) {
	 * List<Integer> combination = new ArrayList<Integer>(); int n=inputList.size();
	 * for (int i = 0; i < n; i++) { combination.add(0); } int i=0; boolean
	 * isContinue=false; do{ //打印一次循环生成的组合 for (int j = 0; j < n; j++) {
	 * System.out.print(inputList.get(j).get(combination.get(j))+", "); }
	 * 
	 * i++; combination.set(n-1, i); for (int j = n-1; j >= 0; j--) { if
	 * (combination.get(j)>=inputList.get(j).size()) { combination.set(j, 0); i=0;
	 * if (j-1>=0) { combination.set(j-1, combination.get(j-1)+1); } } }
	 * isContinue=false; for (Integer integer : combination) { if (integer != 0) {
	 * isContinue=true; } } }while (isContinue); }
	 */
}
